<?php


namespace app\api\controller\v1;


use app\adminapi\controller\AuthController;
use app\exceptions\AuthException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Iai\V20180301\Models\VerifyFaceRequest;
use TencentCloud\Iai\V20200303\IaiClient;
use TencentCloud\Iai\V20200303\Models\CreatePersonRequest;
use TencentCloud\Iai\V20200303\Models\SearchFacesRequest;


class Face extends AuthController
{
    /**
     * 人脸认证相关接口
     * 文档地址: https://cloud.tencent.com/document/product/867/44983
     * @return mixed
     */
    public function faceVerify(){
        $where = $this->request->getMore([
            ['image',''],//base64
            ['PersonId',''],//用户唯一标识
        ]);
        try {
            $cred = new Credential("AKIDb093g27wUlmW1FBqELBj23nFQLzMSJtC", "2oIVR5TaPxFFyQdZEQQCj02oLcdPvCD1");//冯平坤
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("iai.tencentcloudapi.com");
            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new IaiClient($cred, "ap-chongqing", $clientProfile);

            $req = new VerifyFaceRequest();
            $params = array(
                "Image" => $where['image'],
                "PersonId" => $where['PersonId']
            );
            $req->fromJsonString(json_encode($params));
            $resp = $client->VerifyFace($req);
            return app()->make('json')->successful($resp);
        }
        catch(TencentCloudSDKException $e) {
           throw new AuthException('验证失败！');
        }

    }

    /**
     * 人脸搜索
     * 文档地址: https://cloud.tencent.com/document/product/867/44994
     * @return void
     */
    public function faceSearch(){
        try {
            $where = $this->request->getMore([
                ['image',''],//base64
            ]);
            $cred = new Credential("AKIDb093g27wUlmW1FBqELBj23nFQLzMSJtC", "2oIVR5TaPxFFyQdZEQQCj02oLcdPvCD1");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("iai.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new IaiClient($cred, "ap-chongqing", $clientProfile);

            $req = new SearchFacesRequest();

            $params = array(
                "GroupIds" => ['20220118','154214'],
                "Image" => $where['image'],
            );
            $req->fromJsonString(json_encode($params));
            $resp = $client->SearchFaces($req);
            return app()->make('json')->successful($resp);

        }
        catch(TencentCloudSDKException $e) {
            throw new AuthException($e->getMessage());
        }
    }


    /**
     * 创建人员
     * 文档地址: https://cloud.tencent.com/document/product/867/45014
     * @return mixed
     */
    public function addFaceUser(array $where){

        try {
            $cred = new Credential("AKIDb093g27wUlmW1FBqELBj23nFQLzMSJtC", "2oIVR5TaPxFFyQdZEQQCj02oLcdPvCD1");
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint("iai.tencentcloudapi.com");

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new IaiClient($cred, "ap-chongqing", $clientProfile);

            $req = new CreatePersonRequest();

            $params = array(
                "GroupId" => "20220118",// 用户组id
                "PersonName" => $where['name'],//名称
                "PersonId" => $where['idcard'],//身份证
                "Gender" => $where['sex'],//1代表男性，2代表女性。
                "Url" => $where['heard']
            );
            $req->fromJsonString(json_encode($params));
            $resp = $client->CreatePerson($req);
            return app()->make('json')->successful($resp);

        }
        catch(TencentCloudSDKException $e) {
            throw new AuthException($e -> getMessage());

        }
    }

}